package com.example.toutiao.utils

import android.graphics.Rect
import android.view.View
import android.view.ViewTreeObserver
import android.widget.LinearLayout
import java.text.SimpleDateFormat
import java.util.*
import java.util.regex.Pattern

object CommonUtils {
    fun replaceBlank(s: String)=s?.replace("\\s".toRegex(), "")
    fun isMobile(mobile: String):Boolean{
        val compile = Pattern.compile("^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}\$")
        val matcher = compile.matcher(mobile)
        return matcher.matches()
    }

    fun getGlobalListener(decorView: View, contentView: View): ViewTreeObserver.OnGlobalLayoutListener? {
        return ViewTreeObserver.OnGlobalLayoutListener{
            val r= Rect()
            decorView.getWindowVisibleDisplayFrame(r)
            val height=decorView.context.resources.displayMetrics.heightPixels
            val diff=height-r.bottom
            if(diff!=0){
                if(contentView.paddingBottom!=diff){
                    contentView.setPadding(0,0,0,diff)
                }
            }else{
                if(contentView.paddingBottom!=0){
                    contentView.setPadding(0,0,0,0)
                }
            }
        }
    }
    fun getTime(date: Date):String{
        val format=SimpleDateFormat("yyyy-MM-dd")
        return format.format(date)
    }

}